package sporteam.web.client;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import sporteam.common.PMF;
import sporteam.web.server.UpdateUser;

@SuppressWarnings("serial")
@PersistenceCapable
public class UserAppData implements Serializable{
	private static final Logger MyLog = Logger.getLogger(UpdateUser.class
			.getName());
	
	
	
	@Persistent
	@PrimaryKey
	private String user_id;
	
	@Persistent
	private boolean firstVisit = true;
	
	@Persistent(serialized = "true")
	private ArrayList<Competition> competitions = new ArrayList<Competition>();

	
	
	public UserAppData() {
		
	}
	
	public UserAppData(String id) {
		this.user_id = id;
	}
	
	
	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

	public boolean isFirstVisit() {
		return firstVisit;
	}

	public void setFirstVisit(boolean firstVisit) {
		this.firstVisit = firstVisit;
	}

	public ArrayList<Competition> getCompetitions() {
		return competitions;
	}

	public void setCompetitions(ArrayList<Competition> competitions) {
		this.competitions = competitions;
	}
	public void addCompetition(Competition competiton){
		this.competitions.add(competiton);
	}
	
	
	public static void updateCompetitonByTrack(String id, Track track){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		UserAppData uad = pm.getObjectById(UserAppData.class, id);
		
		for(Competition competiton : uad.getCompetitions()){
			//if the competition is open
			if(competiton.isClosed() == false){
				competiton.updateScoreByTrack(track, id);
				//updates other runner competition aswell
				updateOtherUserCompetition(track, competiton.getOtherId(id));
			}
		}
		pm.makePersistent(uad);
		pm.close();
	}
	
	private static void updateOtherUserCompetition(Track track, String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		UserAppData uad = pm.getObjectById(UserAppData.class, id);
		for(Competition competiton : uad.getCompetitions()){
			if(competiton.isClosed() == false){
				competiton.updateScoreByTrack(track, id);
			}
		}
		pm.makePersistent(uad);
		pm.close();
	}

	public static void addUserAppDataToDataStore(String id){
		
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		UserAppData uad = new UserAppData(id);
		
		//for debug (barmako and tester jones ids)
		MyLog.warning("Checking if user is barmako or test id is: " + id);
		if(id.equals("1507604362") || id.equals("100003730751502")){
			MyLog.warning("Creating new competition");
			uad.addCompetition(makeCompetition("1507604362", "100003730751502"));
			MyLog.warning(uad.getCompetitions().get(0).toString());
		}
		MyLog.warning("Competitions size: " + uad.getCompetitions().size());
		
		pm.makePersistent(uad);
		pm.close();
		
	}
	
	private static Competition makeCompetition(String id1, String id2) {
		Competition comp = new Competition(id1, id2, CompetitionType.DISTANCE);
		return comp;
	}

	public String printCompetitions(){
		String returnString = "";
		for(Competition comp : this.competitions){
			returnString += "Competition: " + comp.toString() + "\n\n";
		}
		return returnString;
	}
}
